/**
 * 
 */
requirejs.config({
    baseUrl:'http://static.auoffice.com/static/js' ,
    waitSeconds: 0,
    paths: {
    	jquery:'libs/jquery.min',
    	validate:'libs/validate/jquery.validate.min',
    	iCheck: 'libs/iCheck/icheck.min',
        //plugins
        base: 'common/base',
    },
    shim: {
    	'validate': {
    		deps: ['jquery'],
    		exports: 'validate'
    	},    	
    	'iCheck': {
    		deps: ['jquery'],
    		exports: 'iCheck'
    	},
    }
});

require([
    'jquery',
 	'common/base',
 	'validate',
 	'iCheck'
],
function($,base) {
	
	$(document).ready(function(){
		$(".i-checks").iCheck({
			checkboxClass:"icheckbox_square-green",
			radioClass:"iradio_square-green",
		});
	});
	
	
	
    //自定义验证规则
	$.validator.addMethod("mobile", function(value, element) {   
    	var mobile_reg = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/;
    	console.log((mobile_reg.test(value)||email_reg.test(value)));
    	return this.optional(element) || (mobile_reg.test(value));
    }, "请输入正确的手机号码");
	//自定义验证规则
	$.validator.addMethod("Newemail", function(value, element) {   
		var email_reg = /^[a-zA-Z0-9][\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
		console.log((mobile_reg.test(value)||email_reg.test(value)));
		return this.optional(element) || (email_reg.test(value));
	}, "请输入正确的邮箱");
    
	//表单验证
	$("#updateForm").validate({
		rules:{
			firstname:{//昵称
				required:true,
				minlength:2,
				maxlength:16,
			},
			email:{//邮箱
				required:false,
				Newemail:false,
				minlength:7,
				maxlength:30,
				remote: {
					url: '/account/checkusername/',
					type: 'post',
					dataType: 'json',
					data: {
						username: function() {
							return $("input[name=email]" ).val();
						},
						user_id: function() {
							return $("input[name=user_id]" ).val();
						}
					}
				}
			},
			mobile:{//手机号码
				required:false,
				mobile:false,
				minlength:11,
				maxlength:11,
				remote: {
					url: '/account/checkusername/',
					type: 'post',
					dataType: 'json',
					data: {
						username: function() {
							return $("input[name=mobile]" ).val();
						},
						user_id: function() {
							return $("input[name=user_id]" ).val();
						}
					}
				}
			},
			sex:{//性别
				required:true,
			},
			is_active:{//状态
				required:true,
			},
			use_type:{//用户类型
				required:true,
			},
		},
		messages:{
			firstname:{
				required:"请输入昵称",
				minlength:"昵称长度为2-16个字符",
				maxlength:"昵称长度为2-16个字符"
			},
			email:{//邮箱
				required:false,
				minlength:"邮箱长度为7-30个字符",
				maxlength:"邮箱长度为7-30个字符",
				Newemail:"请输入正确格式的邮箱",
				remote:"邮箱已存在(注册)"
			},
			mobile:{//手机号码
				required:false,
				mobile:false,
				minlength:"请输入正确格式的手机号码",
				maxlength:"请输入正确格式的手机号码",
				remote:"手机号已存在(注册)",
			},
			sex:{//性别
				required:"请选择性别",
			},
			is_active:{//状态
				required:"请选择状态",
			},
			use_type:{//用户类型
				required:"请选择类型",
			},
		},
		submitHandler:function(){
			base.services.post('account', 'updateUser', $("#updateForm").serialize(), function(code, msg, data) {
				if (code === base.services.CODE_SUCC) {
					base.layui.alert("成功信息","保存成功");
				} else {
					base.layui.alert("错误信息",msg);
				}
			});
		}
	});
	//点击确定按钮
	$(".save").click(function(){
		$("#updateForm").submit();
	});
});